﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using System.Data;
using System.Windows.Forms;


public class orcl
{

    public static string user;
    public static string pass;
    public static string imagePath = "image/";
    //private static string _connectionStr = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=" +
    //            "1521" + ")))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" +
    //            "ORCL" + ")));User Id=" +
    //            "u0912104" + ";Password=" +
    //            "u0912104" + ";";
    public static OracleConnection conn;// = new OracleConnection(orcl._connectionStr);

	~orcl()
	{
		try
		{
			conn.Close();
		}
		catch
		{
        }
    }
    //public static string connectionStr
    //{
    //    get
    //    {
    //        return orcl._connectionStr;
    //    }
    //    set
    //    {
    //        orcl._connectionStr = value;
    //    }
    //}
    public orcl()
    {
        orcl.conn.Open();
    }

    #region ExecuteQuery
    public static DataTable ExecuteQuery(String query)
    {

        DataTable table = new DataTable();
        try
        {
            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = query;
                OracleDataAdapter adapter = new OracleDataAdapter();
                adapter.SelectCommand = command;
                adapter.Fill(table);
            }

            catch (OracleException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
        return table;
    }
    #endregion

    #region ExecuteNoneQuery
    public static int ExecuteNoneQuery(String query)
    {
        int n;
        try
        {

            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = query;
                n = command.ExecuteNonQuery();
                return n;
            }

            catch (OracleException ex)
            {
                MessageBox.Show(ex.Message);
                return -2;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return -2;
        }
    }

    #endregion

    #region ExecuteQuery
    public static DataTable ExecuteQueryThrowEx(String query)
    {
        DataTable table = new DataTable();
        try
        {
            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = query;
                OracleDataAdapter adapter = new OracleDataAdapter();
                adapter.SelectCommand = command;
                adapter.Fill(table);
            }
            catch (OracleException ex)
            {
                throw ex;
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
        return table;
    }
    #endregion

    #region ExecuteNoneQuery
    public static int ExecuteNoneQueryThrowEx(String query)
    {
        int n;
        try
        {
            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = query;
                n = command.ExecuteNonQuery();
                return n;
            }

            catch (OracleException ex)
            {
                throw ex;
            }            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion

    public static void ExecuteStoredProcedure(String procName, OracleParameter[] param)
    {
        try
        {
            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = procName;
                for (int i = 0; i < param.Length; i++)
                {
                    command.Parameters.Add(param[i]);
                }
                command.ExecuteNonQuery();
            }

            catch (OracleException ex)
            {
                throw ex;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #region CheckRoleOfUser
    public static string checkRoleOfUser()
    {
        DataTable dt = new DataTable();

        dt = orcl.ExecuteQuery("select GRANTED_ROLE  from user_role_privs");
        string role = "NGUOIDIBAU";

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i][0].ToString() != "NGUOIDIBAU")
                return dt.Rows[i][0].ToString();
        }

        return role;
    }
    #endregion
}